package com.osc.exam.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.osc.exam.config.PageBean;
import com.osc.exam.constant.GlobalConstant;
import com.osc.exam.entity.AdminResponse;
import com.osc.exam.entity.domain.Practice;
import com.osc.exam.entity.domain.Progress;
import com.osc.exam.entity.domain.Student;
import com.osc.exam.entity.vo.IdVO;
import com.osc.exam.entity.vo.InterviewVO;
import com.osc.exam.entity.vo.StudentVO;
import com.osc.exam.service.PracticeService;
import com.osc.exam.service.ProgressScaleService;
import com.osc.exam.service.StudentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/student")
@Api("学生信息相关接口")
public class StudentController {

    @Autowired
    private StudentService studentService;
    @Autowired
    private PracticeService practiceService;
    @Autowired
    private ProgressScaleService progressScaleService;

    /**
     * 学生信息录入功能
     *
     * @param studentVO
     * @return
     */
    @PostMapping("/insert-data")
    @ApiOperation(value = "学生信息录入功能--左坤", notes = "学生信息录入功能")
    @RequiresPermissions("Create")
    public ResponseEntity insterData(@RequestBody StudentVO studentVO) {
        studentService.insterData(studentVO);
        return ResponseEntity.status(HttpStatus.OK)
                .body(AdminResponse.result(GlobalConstant.SUCCESS, "信息录入成功!"));
    }

    /**
     * 查询录入的全部学生信息
     *
     * @param name
     * @param status
     * @param offset
     * @param limit
     * @return
     */
    @GetMapping("/student-all")
    @ApiOperation(value = "查询录入的全部学生信息--左坤", notes = "查询录入的全部学生信息")
    @RequiresPermissions("Retrieve")
    public ResponseEntity studentDataAll(@RequestParam(value = "name") String name,
                                         // 意向城市
                                         @RequestParam(value = "cityApplied") String cityApplied,
                                         // 岗位
                                         @RequestParam(value = "positionApplied") String positionApplied,
                                         @RequestParam(value = "status", defaultValue = "") String status,
                                         @RequestParam(value = "offset", defaultValue = "0") int offset,
                                         @RequestParam(value = "limit", defaultValue = "0") int limit) {
        PageHelper.startPage(offset, limit);
        List<Student> studentList = studentService.queryAllByLimit(cityApplied, positionApplied, name, status);
        PageInfo<Student> info = new PageInfo<Student>(studentList, 2);
        return ResponseEntity.status(HttpStatus.OK).body(AdminResponse.result(GlobalConstant.SUCCESS, "查询成功", info));
    }


    /**
     * 学生信息编辑功能
     * @param student
     * @return
     */
    @PostMapping("/update-data")
    @ApiOperation(value = "学生信息编辑功能--赵琳", notes = "学生信息编辑功能")
    @RequiresPermissions("Update")
    public ResponseEntity updateData(@RequestBody Student student) {
        boolean res = studentService.update(student);
        if (res) {
            return ResponseEntity.status(HttpStatus.OK)
                    .body(AdminResponse.result(GlobalConstant.SUCCESS, "编辑学生信息成功!"));
        }
        return ResponseEntity.status(HttpStatus.OK)
                .body(AdminResponse.result(GlobalConstant.FAIL, "编辑学生信息失败!"));
    }

    /**
     * 删除学生信息
     *
     * @param id
     * @return
     */
    @PostMapping("/del-data")
    @ApiOperation(value = "删除学生信息功能--左坤", notes = "删除学生信息功能")
    @RequiresPermissions("Delete")
    public ResponseEntity delData(@RequestBody IdVO id) {
        studentService.deleteData(id.getId());
        return ResponseEntity.status(HttpStatus.OK)
                .body(AdminResponse.result(GlobalConstant.SUCCESS, "删除成功!"));
    }

    /**
     * 添加面试评论信息
     *
     * @param interviewVO
     * @return
     */
    @PostMapping("/interview-result")
    @ApiOperation(value = "添加面试分数和评价信息--左坤", notes = "添加面试分数和评价信息")
    @RequiresPermissions("Create")
    public ResponseEntity interviewResult(@RequestBody InterviewVO interviewVO) {
        studentService.interviewResult(interviewVO);
        return ResponseEntity.status(HttpStatus.OK)
                .body(AdminResponse.result(GlobalConstant.SUCCESS, "完成评价!"));
    }

    /**
     * 各论面试学生信息
     *
     * @param offset
     * @param limit
     * @return
     */
    @GetMapping("/progress-all")
    @ApiOperation(value = "各论面试学生信息--左坤", notes = "各论面试学生信息")
    @RequiresPermissions("Retrieve")
    public ResponseEntity prgoressAll(@RequestParam(value = "status") String status,
                                      @RequestParam(value = "offset") int offset,
                                      @RequestParam(value = "limit") int limit) {
        PageHelper.startPage(offset, limit);
        List<Student> studentList = studentService.selectAll(status);
        PageInfo<Student> info = new PageInfo<Student>(studentList, 2);
        return ResponseEntity.status(HttpStatus.OK).body(AdminResponse.result(GlobalConstant.SUCCESS, "查询成功 !", info));

    }

    /**
     * 查询实习信息
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/practice")
    @ApiOperation(value = "查询实习信息--赵琳", notes = "查询实习信息")
    @RequiresPermissions("Retrieve")
    public ResponseEntity practice(@RequestParam("pageNum") int pageNum, @RequestParam("pageSize") int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Student> students = studentService.queryTraineeByPage();
        PageInfo<Student> info = new PageInfo<Student>(students, 2);
        return ResponseEntity.status(HttpStatus.OK)
                .body(AdminResponse.result(GlobalConstant.SUCCESS, "查询实习生信息成功!", info));
    }

    /**
     * 添加实习分数和评价信息
     * @param practice
     * @return
     */
    @PostMapping("practice-result")
    @ApiOperation(value = "添加实习分数和评价信息--赵琳", notes = "添加实习分数和评价信息")
    @RequiresPermissions("Create")
    public ResponseEntity practiceResult(@RequestBody Practice practice) {
        boolean res = practiceService.insert(practice);
        if (practice.getStatus() == 11) {
            if (practice.getScore() == 1) {
                studentService.updateStatus(practice.getStudentId(), 10, new Date());
            } else {
                studentService.updateStatus(practice.getStudentId(), 9, new Date());
            }
        }
        if (res) {
            return ResponseEntity.status(HttpStatus.OK)
                    .body(AdminResponse.result(GlobalConstant.SUCCESS, "添加实习评价成功!"));
        }
        return ResponseEntity.status(HttpStatus.OK)
                .body(AdminResponse.result(GlobalConstant.FAIL, "添加实习评价失败!"));
    }

    public ResponseEntity getPractice(@RequestParam("id") int id) {
        List<Practice> practices = practiceService.getPracById(id);
        return ResponseEntity.status(HttpStatus.OK)
                .body(AdminResponse.result(GlobalConstant.SUCCESS, "查看实习评价成功!", practices));
    }

    /**
     * 查询面试评价信息
     * @param id
     * @return
     */
    @GetMapping("getProgress")
    @ApiOperation(value = "查询面试评价信息--赵琳", notes = "查询面试评价信息")
    @RequiresPermissions("Retrieve")
    public ResponseEntity getProgress(@RequestParam("id") int id) {
        List<Progress> progresses = progressScaleService.getInterById(id);
        return ResponseEntity.status(HttpStatus.OK)
                .body(AdminResponse.result(GlobalConstant.SUCCESS, "查看面试评价成功!", progresses));
    }

    /**
     * 文件下载功能
     * @param file
     * @return
     */
    @PostMapping("/upload")
    @ApiOperation(value = "文件上传功能--左坤", notes = "文件上传功能")
    public ResponseEntity upload(@RequestBody MultipartFile file) {
        studentService.uploadFile(file);
        return ResponseEntity.status(HttpStatus.OK)
                .body(AdminResponse.result(GlobalConstant.SUCCESS, "文件上传成功!"));

    }

    /**
     * 文件下载功能
     * @param fileName
     * @param response
     * @return
     */
    @GetMapping("/download")
    @ApiOperation(value = "文件下载功能--左坤", notes = "文件下载功能")
    public ResponseEntity download(@RequestParam("fileName") String fileName, HttpServletResponse response) {
        studentService.download(fileName, response);
        return ResponseEntity.status(HttpStatus.OK).body(AdminResponse.result(GlobalConstant.SUCCESS, "文件下载成功!"));
    }

}
